# encoding: utf-8
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
    :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :lastname, :address, :phone, :cultural_institution, :state, :username, :role, :created_at, :updated_at

  belongs_to :obj_cultural_institution, :class_name => 'CulturalHeritage::CulturalInstitution',  :foreign_key => 'cultural_institution'
  belongs_to :obj_role, :class_name => 'Security::UserRole',  :foreign_key => 'role'

  

  default_scope where("deleted = 0")

  def active_for_authentication?
    super && (state == 1)
  end

  def inactive_message
    if (state != 1)
      "Usuario no permitido."
    else 
      super # Use whatever other message
    end
  end

   def str_state
    if state == 0
      return "Registrado"
    end
    if state == 1
      return "Aprobado"
    end
    if state == 2
      return "Rechazado"
    end
    if state == 3
      return "Suspendido"
    end
  end

   def str_complete_name
     return ''+name+' '+lastname+''
   end
  
  protected
  def self.find_for_database_authentication(warden_conditions)
    conditions = warden_conditions.dup
    login = conditions.delete(:username)
    where(conditions).where(["lower(username) = :value", { :value => login.downcase }]).first
  end

end
